package easy4j.framework.example;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.*;

import java.io.File;


/**
 * @author bjliuyong
 * @version 1.0
 * @created date 16-10-10
 */
public class Starter {

    public static void main(String args[]) throws Exception{
        int port = 8080 ;
        final Server server = new Server(port);
        final WebAppContext root = new WebAppContext();

        root.setParentLoaderPriority(false); // Use parent class loader
        root.setContextPath("/");
        root.setConfigurations(new Configuration[] {
                new AnnotationConfiguration(),
                new WebXmlConfiguration(),
                new WebInfConfiguration(),
                new PlusConfiguration(),
                new MetaInfConfiguration(),
                new FragmentConfiguration(),
                new EnvConfiguration()});

        String webappDirLocation = "src/main/webapp/"; // POM structure in dev env
        final File file = new File(webappDirLocation);
        if (!file.exists()) {
            webappDirLocation = "."; // production environment
        }

        //String webappDirLocation = easy4j.framework.example.Starter.class.getClassLoader().getResource("/").getPath() ;
        root.setResourceBase(webappDirLocation);

       /* root.setClassLoader(DispatcherServletInitializer.class.getClassLoader());*/
        //root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
        //root.setResourceBase(webappDirLocation);
        server.setHandler(root);
        try {
            server.start();
        } catch (final Exception e) {
            e.printStackTrace();

            System.exit(-1);
        }

        /*serverScheme = Latkes.getServerScheme();
        serverHost = Latkes.getServerHost();
        serverPort = Latkes.getServerPort();
        final String contextPath = Latkes.getContextPath();

        try {
            Desktop.getDesktop().browse(new URI(serverScheme + "://" + serverHost + ":" + serverPort + contextPath));
        } catch (final Throwable e) {
            // Ignored
        }*/

        server.join();
    }
}
